#include "AABB.h"

float AABB::getSize(const int axis) const
{
	return max.get(axis) - min.get(axis);
}

float AABB::getHullSize(void) const
{
	const float sizeX = max.x - min.x;
	const float sizeY = max.y - min.y;
	const float sizeZ = max.z - min.z;
	return 2 * (sizeX * sizeY + sizeX * sizeZ + sizeY * sizeZ);
}

float AABB::getLeftHullSize(const float split, const int axis) const
{
//	float maxX = max.x;
//	float maxY = max.y;
//	float maxZ = max.z;
//	if (axis == X)
//		maxX = split;
//	else if (axis == Y)
//		maxY = split;
//	else
//		maxZ = split;
	
	const float maxX = (axis == X) ? split : max.x;
	const float maxY = (axis == Y) ? split : max.y;
	const float maxZ = (axis == Z) ? split : max.z;

	const float sizeX = maxX - min.x;
	const float sizeY = maxY - min.y;
	const float sizeZ = maxZ - min.z;
	return 2 * (sizeX * sizeY + sizeX * sizeZ + sizeY * sizeZ);
}

float AABB::getRightHullSize(const float split, const int axis) const
{
//	float minX = min.x;
//	float minY = min.y;
//	float minZ = min.z;
//	if (axis == X)
//		minX = split;
//	else if (axis == Y)
//		minY = split;
//	else
//		minZ = split;
	
	const float minX = (axis == X) ? split : min.x;
	const float minY = (axis == Y) ? split : min.y;
	const float minZ = (axis == Z) ? split : min.z;

	const float sizeX = max.x - minX;
	const float sizeY = max.y - minY;
	const float sizeZ = max.z - minZ;
	return 2 * (sizeX * sizeY + sizeX * sizeZ + sizeY * sizeZ);
}

int AABB::getMaxAxis(void) const
{
	const float sizeX = max.x - min.x;
	const float sizeY = max.y - min.y;
	const float sizeZ = max.z - min.z;
	
	return (sizeX > sizeY) ? ((sizeX > sizeZ) ? X : Z) : ((sizeY > sizeZ) ? Y : Z);
}

AABB AABB::createLeft(const float split, const int axis) const
{
	const float maxX = (axis == X) ? split : max.x;
	const float maxY = (axis == Y) ? split : max.y;
	const float maxZ = (axis == Z) ? split : max.z;

	return AABB(min, SPoint(maxX, maxY, maxZ));
}

AABB AABB::createRight(const float split, const int axis) const
{
	const float minX = (axis == X) ? split : min.x;
	const float minY = (axis == Y) ? split : min.y;
	const float minZ = (axis == Z) ? split : min.z;

	return AABB(SPoint(minX, minY, minZ), max);
}
